package leetcode.editor.cn.dsa13_mergesort;
//给定一个 n 个元素有序的（升序）整型数组 nums 和一个目标值 target ，
// 写一个函数搜索 nums 中的 target，如果目标值存在返回下标，否则返回 -1。

public class BinarySearch704_1 {
    public static void main(String[] args) {
        Solution solution = new BinarySearch704_1().new Solution();
        int[] nums = {-1,0,3,5,9,12};
        System.out.println(solution.search(nums, 9)); //4
        System.out.println(solution.search(nums, 2)); //-1
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int search(int[] nums, int target) {
            int start = 0, end = nums.length - 1;
            while (start <= end) {
                int m = start + (end - start)/2;
                if (nums[m] == target) return m;
                if (nums[m] < target) {
                    start = m + 1;
                } else {
                    end = m - 1;
                }
            }
            return -1;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)
}